package bb.lanxing.lib.devices.core.scanner;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractDeviceScanner<DEVICE> implements DeviceScanner {
    private static final int ON_STOP_SCAN = 17;
    private static final int ON_START_SCAN = 18;
    private static final int ACTION_STOP_SCAN_DELAY = 19;
    private static final int ACTION_DEVICE = 20;
    private static final int SCAN_INTERVAL = 500;
    private final List<String> addressKeys = new ArrayList();
    protected Context applicationContext;
    private boolean isScanning;
    private long lastNotifyMillis;
    protected DeviceScanner.ScannerCallback scannerCallback;
    protected ScannerHandler scannerHandler;

    protected abstract boolean doStartScan();

    public AbstractDeviceScanner(Context context, DeviceScanner.ScannerCallback<DEVICE> scannerCallback) {
        this.applicationContext = context;
        this.scannerCallback = scannerCallback;
    }

    @Override
    public boolean startScan() {
        return startScan(-1L);
    }

    @Override
    public boolean startScan(long j) {
        if (this.scannerHandler == null) {
            this.scannerHandler = new ScannerHandler(this);
        }
        boolean doStartScan = doStartScan();
        this.isScanning = doStartScan;
        if (doStartScan) {
            this.addressKeys.clear();
            ScannerHandler scannerHandler = this.scannerHandler;
            scannerHandler.sendMessage(scannerHandler.obtainMessage(ON_START_SCAN, this));
            if (j > 0) {
                this.scannerHandler.sendEmptyMessageDelayed(ACTION_STOP_SCAN_DELAY, j);
            }
        }
        return this.isScanning;
    }

    @Override
    public boolean isScanning() {
        return this.isScanning;
    }

    @Override
    public boolean stopScan() {
        ScannerHandler scannerHandler = this.scannerHandler;
        if (scannerHandler != null) {
            scannerHandler.removeCallbacksAndMessages(null);
            ScannerHandler scannerHandler2 = this.scannerHandler;
            scannerHandler2.sendMessage(scannerHandler2.obtainMessage(ON_STOP_SCAN, this));
        }
        this.isScanning = false;
        return false;
    }

    public void notifyScannedDevice(DEVICE device) {
        ScannerHandler scannerHandler = this.scannerHandler;
        if (scannerHandler != null) {
            scannerHandler.obtainMessage(ACTION_DEVICE, 0, 0, device).sendToTarget();
        }
    }

    @Override
    public void release() {
        this.applicationContext = null;
        this.scannerCallback = null;
        this.scannerHandler = null;
    }

    public boolean shouldNotify(String str) {
        boolean z = !this.addressKeys.contains(str) || SystemClock.uptimeMillis() - this.lastNotifyMillis > SCAN_INTERVAL;
        if (!this.addressKeys.contains(str)) {
            this.addressKeys.add(str);
        }
        this.lastNotifyMillis = SystemClock.uptimeMillis();
        return z;
    }

    public static class ScannerHandler extends Handler {
        WeakReference<AbstractDeviceScanner> scannerRef;

        public ScannerHandler(AbstractDeviceScanner abstractDeviceScanner) {
            super(Looper.getMainLooper());
            this.scannerRef = new WeakReference<>(abstractDeviceScanner);
        }

        @Override
        public void handleMessage(Message message) {
            WeakReference<AbstractDeviceScanner> weakReference = this.scannerRef;
            DeviceScanner.ScannerCallback scannerCallback = null;
            AbstractDeviceScanner abstractDeviceScanner = weakReference != null ? weakReference.get() : null;
            if (abstractDeviceScanner != null) {
                scannerCallback = abstractDeviceScanner.scannerCallback;
            }
            if (abstractDeviceScanner == null || scannerCallback == null) {
                return;
            }
            switch (message.what) {
                case ON_STOP_SCAN -> {
                    scannerCallback.onStopScan();
                }
                case ON_START_SCAN -> {
                    scannerCallback.onStartScan();
                }
                case ACTION_STOP_SCAN_DELAY -> {
                    abstractDeviceScanner.stopScan();
                }
                case ACTION_DEVICE -> {
                    scannerCallback.onDevice(message.obj);
                }
                default -> {
                }
            }
        }
    }
}
